<?php

class Add_Publish_Article_SavePost_Validate_Attachment_Image extends Ext_Validate_Abstract
{
    protected $_articleId;
    protected $_image = array();

    const RECORD_NOT_FOUND   = 'recordNotFound';
    protected $_messageTemplates = array(
        self::RECORD_NOT_FOUND   => "Image data record not found.",
    );
    
    public function __construct($articleId, $image)
    {
        if(!is_array($image)){
            $image = array();
        }
        $this->_articleId       = $articleId;
        $this->_image           = $image;
        
        $this->_messager = new Ext_Messager($this->_messageTemplates);
    }
    
    public function isValid()
    {
        foreach($this->_image as $key=>$val){
            $attachmentImageId = $key;
            
            $image = App_Model_DbTable_Attachment_Image::getInstance()->getRowById($attachmentImageId);
            if(!$image){
                $this->addMessage($this->_messager->addMessage(self::RECORD_NOT_FOUND));
                return false;
            }
            if($image['articleId'] != $this->_articleId){
                $this->addTitleMessage('Image',"Access denied.");
                return false;
            }
        }
        
        return true;
    }
}
